package com.nacos.cert.server.controller;

import com.alibaba.fastjson.JSON;
import com.nacos.common.api.servicedate.DbServiceUsersApi;
import com.nacos.common.dto.UserDTO;
import com.nacos.common.entity.User;
import com.nacos.common.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@Slf4j
public class UserController {

    @Autowired
    JWTUtils jwtUtils;
    @Autowired
    DbServiceUsersApi dbServiceUsersApi;

    @RequestMapping("/client/login")
    public ResponseEntity<String> login(HttpServletResponse response, @RequestBody User user) {
        log.info("请求报文={}", JSON.toJSONString(user));
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (user.getPassword() == null || user.getUsername() == null) {
                resultMap.put("state", false);
                resultMap.put("msg", "认证失败，请重新输入密码");
                return new ResponseEntity<>(JSON.toJSONString(resultMap), HttpStatus.OK);
            }
            //根据请求参数查询数据库数据
            UserDTO userDTO = new UserDTO();
            userDTO.setUsername(user.getUsername());
            //调用数据库微服务
            UserDTO userinfo = dbServiceUsersApi.getUserInfo(userDTO);
            log.info("查库结果={}", JSON.toJSONString(userinfo));
            if (userinfo == null || !userinfo.getPassword().equals(user.getPassword())) {
                resultMap.put("state", false);
                resultMap.put("msg", "认证失败，请重新输入密码");
                return new ResponseEntity<>(JSON.toJSONString(resultMap), HttpStatus.OK);
            }

            Map<String, String> payload = new HashMap<>();
            payload.put("id", String.valueOf(userinfo.getId()));
            payload.put("username", userinfo.getUsername());
            payload.put("password", userinfo.getPassword());
            //生成Token令牌
            String token = jwtUtils.getToken(payload);
            resultMap.put("state", true);
            resultMap.put("msg", "认证成功");
            resultMap.put("token", token);
            response.setHeader("Authorization", token);
            Cookie cookie = new Cookie("Authorization", token);
            response.addCookie(cookie);
            log.info("返回报文={}", JSON.toJSONString(resultMap));
            return new ResponseEntity<>(JSON.toJSONString(resultMap), HttpStatus.OK);
        } catch (Exception e) {
            log.error("登录异常",e);
            resultMap.put("state", false);
            resultMap.put("msg", "登录失败");
            log.info("返回报文={}", JSON.toJSONString(resultMap));
            return new ResponseEntity<>(JSON.toJSONString(resultMap), HttpStatus.OK);
        }
    }

    @RequestMapping("/client/updateUserPassword")
    public ResponseEntity<String> updateUserPassword(HttpServletResponse response, @RequestBody User user) {
        log.info("请求报文={}", JSON.toJSONString(user));
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (user.getPassword() == null || user.getUsername() == null) {
                resultMap.put("state", false);
                resultMap.put("msg", "更新失败，新密码为空");
                return new ResponseEntity<>(JSON.toJSONString(resultMap), HttpStatus.OK);
            }
            //根据请求参数查询数据库数据
            UserDTO userDTO = new UserDTO();
            userDTO.setUsername(user.getUsername());
            userDTO.setPassword(user.getPassword());

            //调用数据库微服务
            dbServiceUsersApi.updateUserInfo(userDTO);
            resultMap.put("state", true);
            resultMap.put("msg", "密码更新成功");
            log.info("返回报文={}", JSON.toJSONString(resultMap));
            return new ResponseEntity<>(JSON.toJSONString(resultMap), HttpStatus.OK);
        } catch (Exception e) {
            log.error("更新密码异常",e);
            resultMap.put("state", false);
            resultMap.put("msg","更新密码失败");
            log.info("返回报文={}", JSON.toJSONString(resultMap));
            return new ResponseEntity<>(JSON.toJSONString(resultMap), HttpStatus.OK);
        }
    }

}
